#!/bin/bash
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/sys_check.sh"
if [ -f /etc/os-release ]; then
   echo "Linux system."
elif [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
   echo "macOS system."
   if [ -f ~/.bash_profile ]; then
      source ~/.bash_profile
    fi
elif current_os_is_windows; then
   echo "Windows system."
else
   echo "Unknown OS"
fi

APP_TARGET_DIR_NAME="soulx-podcast"

echo "lmdprogress:3"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_conda.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_run.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_and_pip_settings.sh"

export CURRENT_APP_PATH="${LMD_APPS_DIR}/${APP_TARGET_DIR_NAME}"
echo "$CURRENT_APP_PATH"
mkdir -p "$CURRENT_APP_PATH"

echo "lmdprogress:8"

if [ -d "$CURRENT_APP_PATH" ]; then
    echo "App Dir: $CURRENT_APP_PATH"
else
    echo "Error, App Dir dose not exist, please retry. $CURRENT_APP_PATH"
    exit 1
fi

echo "lmdprogress:12"

export GIT_LFS_SKIP_SMUDGE=1

export GIT_HOST=${GITHUB_PROXY}"https://github.com"
export GIT_REPO_URL="https://gitee.com/lmdown/SoulX-Podcast"
export GIT_REPO_BRANCH="master"
export LOCAL_REPO_PATH="$CURRENT_APP_PATH"
sh "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/git_clone_and_fetch.sh"

echo "lmdprogress:40"

cd "$CURRENT_APP_PATH"
CONDA_PREFIX="./venv"

if [ -d "$CONDA_PREFIX" ]; then
    echo "found ./venv. remove it. "
    rm -rf "$CONDA_PREFIX"
fi

export PYTHON_VER_ENV=3.12
conda_create_env $CONDA_PREFIX python=$PYTHON_VER_ENV pip=25.2 -y

echo "lmdprogress:50"

grep -vE 'torch==|torchaudio==|triton|onnxruntime-gpu|gradio' requirements.txt > requirements-temp.txt

conda_run_pip_install -r requirements-temp.txt

echo "lmdprogress:55"

if command -v nvidia-smi &>/dev/null; then
    echo "install onnxruntime-gpu"
    conda_run_pip_install onnxruntime-gpu
else
    echo "install onnxruntime" 
    conda_run_pip_install onnxruntime
fi

echo "lmdprogress:56"

if current_os_is_windows; then
   conda_run_pip_install triton-windows
fi

echo "lmdprogress:57"

install_torch() {
    if [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
        source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_torch_mac.sh"
    elif current_os_is_windows; then
        source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_torch_multi_method.sh"
    fi
}
install_torch

echo "lmdprogress:60"

if [ -z "$HF_ENDPOINT" ] && [ -n "$HF_MIRROR" ]; then
    export HF_ENDPOINT="$HF_MIRROR"
fi

echo "lmdprogress:80"
conda_run_pip_install gradio==5.49.1
echo "lmdprogress:81"
conda_run_pip_install huggingface_hub hf_xet modelscope

echo "lmdprogress:85"

export XDG_CACHE_HOME="${CURRENT_APP_PATH}/XDG_CACHE_HOME"
export XDG_CONFIG_HOME="${CURRENT_APP_PATH}/XDG_CONFIG_HOME"
export XDG_DATA_HOME="${CURRENT_APP_PATH}/XDG_DATA_HOME"
export XDG_STATE_HOME="${CURRENT_APP_PATH}/XDG_STATE_HOME"

# model
export HF_HOME="$CURRENT_APP_PATH/tmp_hf_home"

if [ -n "$HF_ENDPOINT" ]; then
    conda_run_modelsople_download "Soul-AILab/SoulX-Podcast-1.7B" "pretrained_models/SoulX-Podcast-1.7B"
    conda_run_modelsople_download "Soul-AILab/SoulX-Podcast-1.7B-dialect" "pretrained_models/SoulX-Podcast-1.7B-dialect"
else 
    conda_run_hf_download "Soul-AILab/SoulX-Podcast-1.7B" --local-dir "pretrained_models/SoulX-Podcast-1.7B"
    conda_run_hf_download "Soul-AILab/SoulX-Podcast-1.7B-dialect" --local-dir "pretrained_models/SoulX-Podcast-1.7B-dialect"
fi

echo "lmdprogress:100"


